<?php 
namespace app\index\model;

use \think\Model;


class User extends Model
{
    public function addUser($data)
    {
        unset($data['vercode']);
        unset($data['repassword']);
        $data['ctime'] = time();
        $data['password'] = md5($data['password']);
        $res = User::save($data);
        if ($res){
            exit(json_encode(['error'=>0,'info'=>'注册成功']));
        }else{
            exit(json_encode(['error'=>1,'info'=>'注册失败']));
        }
    }

    public function checkUser($data)
    {
        unset($data['vercode']);
        $data['password'] = md5($data['password']);
        $res = User::get($data);
        if ($res){
            if($res->status != 1){
                exit(json_encode(['error'=>1,'info'=>'账号未激活,请激活账号']));
            }
            session('uid',$res['uid']);
            session('face',$res['face']);
            session('nickname',$res->nickname);
            exit(json_encode(['error'=>0,'info'=>'欢迎回来']));
        }else{
            exit(json_encode(['error'=>1,'info'=>'账号或密码错误']));
        }
    }
    public function updateUser($data)
    {
        if(isset($data['repassword'])){
            unset($data['repassword']);
            $data['password'] = md5($data['password']);
        }
        $res = User::update($data);
        if ($res){
            if(isset($data['nickname'])) {
                session('nickname', $res['nickname']);
            }
            exit(json_encode(['error'=>0,'info'=>'修改成功']));
        }else{
            exit(json_encode(['error'=>1,'info'=>'修改失败']));
        }
    }

}